home *** CD-ROM | disk | FTP | other *** search
- Path: GEOMAT.math.uni-hamburg.de!not-for-mail
- From: ms5a018@GEOMAT.math.uni-hamburg.de (Christian Duehl)
- Newsgroups: comp.lang.c
- Subject: Re: scanf/gets interaction ?
- Date: 27 Mar 1996 11:10:58 GMT
- Organization: University of Hamburg -- Germany
- Message-ID: <4jb7o2$5n1@rzsun02.rrz.uni-hamburg.de>
- References: <4j6joa$6ve@muller.loria.fr>
- NNTP-Posting-Host: rzaixsrv21-fi.rrz.uni-hamburg.de
- X-Newsreader: TIN [UNIX 1.3 950824BETA PL0]
-
- Denis B. Roegel (roegel@loria.fr) wrote:
- : I have the following program:
- :
- : #include <stdio.h>
- :
- : main(){
- : int n;
- : char num[10];
- : char*r;
- : fflush(stdin);
- : printf("n: ");scanf("%i",&n);
- : printf("Numero ? ");r = gets(num);
- : printf("Bien recu!\n");
- : }
- :
- : which I compile with gcc on SunOS. The program asks for a first number n which
- : I enter. But I never get a chance of entering a second one. Why is this so ?
- :
- : Thanks in advance,
- :
- : Denis
-
- Perhaps there is a loop missing :-)
-
- (In K&R I read that fflush with an input stream has a not defined effect...)
-
- I think you could want your program so:
-
- #include <stdio.h>
-
- int main(void)
- {
- int n;
- char num[10]; /* What happens if the input is longer than 9 chars???!!! */
- char *r;
-
- /* fflush(stdin); */ /* undefined effect ... (K&R) */
- printf("n: ");
- scanf("%i",&n);
- r = gets(num);
- while (n>0) {
- printf("Numero ? ");
- r = gets(num);
- printf("Bien recu!\n");
- n--;
- }
-
- return 0;
- } /* main */
-
- ------------------------
-
- the function main has to be from type int, ...(void)... is better then ...()...
- IMHO.
-
- I hope that helps...
- Christian.
- --
-
- -------------------------------------------------------------------------
- Christian Duehl e-mail: duehl@geomat.math.uni-hamburg.de
- (duehl@tu-harburg.d400.de)
- WWW-Homepage: http://www.math.uni-hamburg.de/home/duehl/homepage.html
-